package com.zhkj.txg.module.mine.popup

import android.content.Context
import android.view.View
import android.view.animation.Animation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.adapter.OrderCouponAdapter
import com.zhkj.txg.module.order.entity.OrderCouponEntity
import razerdp.basepopup.BasePopupWindow

class OrderCouponPopupWindow(
    context: Context,
    private val onCouponSelected: (type: Int, uiPosition: Int, coupon: OrderCouponEntity) -> Unit
) : BasePopupWindow(context) {

    companion object {
        val TYPE_SHOP = 0
        val TYPE_PLATFORM = 1
    }

    private lateinit var couponAdapter: OrderCouponAdapter

    var type: Int = TYPE_SHOP
    var uiPosition: Int = -1

    override fun onCreateShowAnimation(): Animation = getTranslateVerticalAnimation(1f, 0f, 300)
    override fun onCreateDismissAnimation(): Animation = getTranslateVerticalAnimation(0f, 1f, 300)
    override fun onCreateContentView(): View = createPopupById(R.layout.popup_order_coupon)
    override fun onViewCreated(contentView: View?) {
        contentView?.let { view ->
            view.findViewById<View>(R.id.ivClose).setOnClickListener { dismiss() }
            view.findViewById<RecyclerView>(R.id.rvOrderCoupon).apply {
                couponAdapter = OrderCouponAdapter().apply {
                    setOnItemClickListener { _, _, position ->
                        dismiss()
                        onCouponSelected(type, uiPosition, data[position])
                    }
                    setOnItemChildClickListener { _, view, position ->
                        when (view.id) {
                            R.id.viewCoupon -> {
                                data[position].shown = data[position].shown.not()
                                notifyItemChanged(position)
                            }
                        }
                    }
                }
                layoutManager = LinearLayoutManager(context)
                adapter = couponAdapter
            }
        }
    }

    fun setData(list: MutableList<OrderCouponEntity>) {
        couponAdapter.setNewData(list)
    }
}